package com.example.medicine.View;

import com.example.medicine.Bean.yian.*;
import com.example.medicine.Mapper.qikanMapper;
import com.example.medicine.Mapper.yian.*;
import com.example.medicine.Mapper.yonghuMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Slf4j
@Controller
public class qiaknView {
    @Autowired
    qikanMapper qikanMapper;

    @Autowired
    private yian_shexiangMapper yian_shexiangMapper;
    @Autowired
    private yian_maizhenMapper yian_maizhenMapper;

    @Autowired
    private yian_jibingMapper yian_jibingMapper;
    @Autowired
    private yian_jianchaMapper yian_jianchaMapper;
    @Autowired
    private yian_chufangMapper yian_chufangMapper;
    @Autowired
    private yian_cankaoMapper yian_cankaoMapper;
    @Autowired
    private yian_xiyiMapper yian_xiyiMapper;
    @Autowired
    private yian_zhengzhuangMapper yian_zhengzhuangMapper;
    @Autowired
    private yonghuMapper yonghuMapper;
    Subject subject = SecurityUtils.getSubject();
    @RequiresPermissions("期刊文献库")
    @RequestMapping("/qikan")
    public String qiaknShow(Model model){
        log.info("期刊文献列表页面");
        model.addAttribute("roleType",yonghuMapper.getTypeByUserId(subject.getPrincipal().toString()));
        return "pages/Periodical/list";
    }
    @RequiresPermissions("期刊文献库")
    @RequestMapping("/qikan/create")
    public String qiaknCreateShow(Model model){
        log.info("期刊文献创建页面");
        model.addAttribute("yian",new yian());
        return "pages/Periodical/create";
    }
    @RequiresPermissions("期刊文献库")
    @RequestMapping("/qikan/batchAdd")
    public String qiaknbatchAddShow(){
        log.info("期刊文献批量导入页面");
        return "pages/Periodical/batchAdd";
    }
    @RequiresPermissions("期刊文献库")
    @RequestMapping("/qikan/edit/{yianId}")
    public String getMyxxById(@PathVariable long yianId, Model model) throws Exception{
        log.info("期刊文献编辑页面");
        yian yian = qikanMapper.getById(yianId);
        List<yian_shexiang> sheList = yian_shexiangMapper.getByYianId(yianId);
        List<yian_maizhen> maiList = yian_maizhenMapper.getByYianId(yianId);
        List<yian_zhengzhuang> zhenghou = yian_zhengzhuangMapper.getByYianId(yianId);  //
        StringBuilder zhBuilder = new StringBuilder();
        if(zhenghou!=null){
            for(yian_zhengzhuang x: zhenghou){
                zhBuilder.append(x.getZhenghou());
            }
        }
        List<yian_jibing> jibing = yian_jibingMapper.getByYianId(yianId);//
        StringBuilder jbBuilder = new StringBuilder();
        if(jibing!=null){
            for(yian_jibing x: jibing){
                jbBuilder.append(x.getMingcheng());
            }
        }
        List<yian_xiyi> xiyiZhenduan = yian_xiyiMapper.getByYianId(yianId);
        StringBuilder xiyibuilder =new StringBuilder();
        if(xiyiZhenduan!=null ){
            for(yian_xiyi x:xiyiZhenduan){
                xiyibuilder.append(x.getName());
            }
        }
        List<yian_jiancha> jiancha = yian_jianchaMapper.getByYianId(yianId);
        List<yian_chufang> chufang = yian_chufangMapper.getByYianId(yianId);
        List<yian_cankao> cankaoList = yian_cankaoMapper.getByYianId(yianId);
        model.addAttribute("yian",yian);
        model.addAttribute("she",sheList);
        model.addAttribute("mai",maiList);
        model.addAttribute("zhenghou",zhBuilder.toString());
        model.addAttribute("jibing",jbBuilder.toString());
        model.addAttribute("xiyiZhenduan",xiyibuilder.toString());
        model.addAttribute("jiancha",jiancha);
        model.addAttribute("chufang",chufang);
        model.addAttribute("cankao",cankaoList);
        model.addAttribute("roleType",yonghuMapper.getTypeByUserId(subject.getPrincipal().toString()));
        return "pages/Periodical/edit";
    }

    @RequiresPermissions("期刊文献库")
    @RequestMapping("/qikan/createbook")
    public String createShow(@RequestParam String bookName, @RequestParam String zuozhe,
                             @RequestParam String publisher, @RequestParam String edition,
                             @RequestParam String publishTime, @RequestParam String isbn, Model model){
        log.info("期刊文献继续录入本书页面");
        yian yian = new yian();
        yian.setShuming(bookName);
        yian.setZuozhe(zuozhe);
        yian.setChubanshe(publisher);
        yian.setChubanshijian(publishTime);
        yian.setBanci(edition);
        yian.setIsbn(isbn);
        model.addAttribute("yian",yian);
        return "pages/Periodical/create";
    }
}
